package saadadb.generationclass;

import java.io.File;
import java.io.FileWriter;

import saadadb.database.Repository;
import saadadb.database.SaadaDBConnector;

/**
 * @author michel
 *
 *07/2009: setAutocommit false
 *11/2013: escape \ in jdb_url (SQLIte on Windows)
 */
public class GenerationClassSaadaDB{
    
    private static String separ = System.getProperty("file.separator");
    /** * @version $Id: GenerationClassSaadaDB.java 1053 2014-03-11 16:45:28Z laurent.mistahl $

     * @param saadadb_conf
     * @throws Exception
     */
    public static void Generation(SaadaDBConnector connector) throws Exception{
    	String nameDB  = connector.getDbname();
    	String rootDir = connector.getRoot_dir().replaceAll("\\\\", "\\\\"+"\\\\");
    	String classpath = (rootDir + separ + "class_mapping").replaceAll("\\\\", "\\\\"+"\\\\");
    	File classFile = new File(classpath + (separ+"SaadaDBStrap.java").replaceAll("\\\\", "\\\\"+"\\\\"));
    	FileWriter writer = new FileWriter(classFile, false);
  
       	writer.write("package generated." +  nameDB + ";\n\n");
    	writer.write("\n");
    	writer.write("import java.sql.DriverManager;\n");
    	writer.write("import java.sql.ResultSet;\n");
    	writer.write("import java.sql.SQLException;\n");
    	writer.write("import java.sql.Statement;\n");
    	writer.write("import saadadb.exceptions.SaadaException;\n");
    	writer.write("import saadadb.exceptions.FatalException;\n");
    	writer.write("import saadadb.database.SaadaDBConnector;\n");
       	writer.write("import saadadb.util.Messenger;\n");
       	writer.write("import cds.astro.Astrocoo;\n");
    	writer.write("import cds.astro.Astroframe;\n");
    	writer.write("import cds.astro.Coo;\n");
    	writer.write("import cds.astro.FK4;\n");
    	writer.write("import cds.astro.FK5;\n");
    	writer.write("import cds.astro.Galactic;\n");
    	writer.write("import cds.astro.ICRS;\n");    	
    	writer.write("\n");
    	writer.write("/**\n");
    	writer.write(" * This class is generated by Saada\n");
    	writer.write(" * @author michel\n");
    	writer.write(" *\n");
    	writer.write(" */\n");
    	writer.write("public class SaadaDBStrap extends SaadaDBConnector {\n");
    	
    	writer.write("	protected SaadaDBStrap(String dbname) {\n");
    	writer.write("		super(dbname);\n");
    	writer.write("	}\n");
    	writer.write("	public  SaadaDBStrap() throws FatalException {\n");
    	writer.write("		super();\n");
    	writer.write("	}\n");
    	
    	writer.write("	/* (non-Javadoc)\n");
    	writer.write("	 * @see saadadb.database.SaadaDBConnector#init()\n");
    	writer.write("	 */\n");
    	writer.write("	protected void init() throws FatalException {\n");
    	/*
    	 * Escape \ for windows
    	 */
    	writer.write("      jdbc_url             = \"" + connector.getJdbc_url().replaceAll("\\\\", "\\\\\\\\") + "\";	\n");
    	writer.write("      jdbc_driver          = \"" + connector.getJdbc_driver() + "\";	\n");
    	writer.write("      jdbc_reader          = \"" + connector.getJdbc_reader() + "\";	\n");
    	if( connector.getJdbc_reader_password() == null ) {
        	writer.write("      jdbc_reader_password = null;\n");
   		
    	}
    	else {
    		writer.write("      jdbc_reader_password = \"" + connector.getJdbc_reader_password() + "\";\n");
    	}
    	writer.write("     readSaadadbTable();\n");
    	writer.write("  }\n");
    	writer.write("}\n");
   	
  
    	writer.flush();
    	writer.close();
    	Compile.compileItWithAnt(connector.getRoot_dir()
    			, "SaadaDBStrap"
    			, (connector.getRepository() + separ + Repository.TMP).replaceAll("\\\\", "\\\\"+"\\\\"));    }
    
}
  
